#include "../GKoSonTools/UNIT/unit.h"
#include "usart.h"

uint32_t    sysDebugFlag = 0xFF;


//万能比较


void All_print(void* A,uint8_t len)
{
	for(uint8_t i=0;i<len;i++)
	printf("%02X ",*( (unsigned char*)A+i   )    );
	printf("\r\n");
}
void All_printc(void* A,uint8_t len)
{
	for(uint8_t i=0;i<len;i++)
	printf("%c",*( (char*)A+i   )    );
	printf("\r\n");
}

void Tatle_All_printc(void* NAME,void* A,uint8_t len)
{
	printf("%s", (char*)NAME );
	for(uint8_t i=0;i<len;i++)
	printf("%c",*( (char*)A+i   )    );
	printf("\r\n");
}

#if 0
void log_err( char* fmt, ...)
{
	va_list args;
	int fmt_result;
	char log_buf[512];

	memset(log_buf , 0x00 , 512);
	
	va_start(args, fmt);
	
	fmt_result = vsprintf(log_buf , fmt, args);
	if (( fmt_result > 0 ) && ( fmt_result < 512))
	{
		err_log(DEBUG_MESSAGE , (uint8_t *)log_buf , fmt_result);
		printf("\033[0;31m");
        printf("[#]");
        printf("[%d%]" , osGetCPUUsage());
        printf_time();\
		printf("%.*s" , fmt_result , log_buf);
		printf("\033[0;39m");
	}
	
	va_end(args);
	 	
}

/*void log_fun(uint8_t level , char* fmt, ...)
{
	va_list args;
	int fmt_result;
	char log_buf[512];
	
	va_start(args, fmt);
	
	fmt_result = vsprintf(log_buf , fmt, args);
	
	if(LOG_BIT_ON(sysDebugFlag,level))
	{
		if(level == ERR)
		{			
			printf("\033[0;31m");
		}
        else if(level == WARN)
		{
        	printf("\033[0;32m");
		}
        else if(level == INFO)
		{
        	printf("\033[0;33m");
		}
		else if(level == DEBUG)
		{
			printf("\033[0;37m");
		}
		
		printf("[#]");
		printf("[%d%]" , osGetCPUUsage());
		printf_time();
		printf("%.*s" , fmt_result,log_buf);
		printf("\033[0;39m");
		
    }
#ifndef BOOTLOADER
	if(  (level == ERR))
	{
		err_log(DEBUG_MESSAGE , (uint8_t *)log_buf , fmt_result);
	}
#endif
	
	va_end(args);

}
*/

////打印数组
//void log_arry(uint32_t level ,unsigned char *pst , unsigned char *arry , unsigned int leng)
//{
//    if(LOG_BIT_ON(sysDebugFlag,level))
//    {
//        if(level == ERR)
//        printf("\033[0;31m");
//        else if(level == WARN)
//        printf("\033[0;32m");
//        else if(level == INFO)
//        printf("\033[0;33m");
//        else if(level == DEBUG)
//        printf("\033[0;37m");
//        printf("[#]");
//        printf("[%d%]" , osGetCPUUsage());
//        printf_time();
//        printf("%s :  [" , pst);
//        for(unsigned int i = 0 ; i < leng ; i++)
//        {
//            printf("%02X " , arry[i]);
//        }
//        printf("]\r\n");
//        printf("\033[0;39m");
//    }
//}



unsigned char str_to_int(unsigned char dData)
{
	if(dData <= '9')
          return dData-'0';
	else if((dData >= 'A')&&(dData <= 'F'))
          return dData+10-'A';
	else
	  return dData+10-'a';
}

//"1234" 4 -->0X12 0X34
int string_to_hex( char *data , int length , char *respone)
{

	if(length%2 != 0)	return -1;

	for( int i = 0 ; i < length/2 ;i++)
	{
		*respone++ = (((str_to_int(data[i*2])<<4)&0xf0)|(str_to_int(data[i*2+1])&0x0f)); 
	}

	return 0;
	
}
//U64形如:0XFFFFFFFFFFFFFFFF 需要8个0XFF 16个'F' 
//网诺过来“1234512345123450” 需要理解成0X1234512345123450
uint64_t atol64( char *str)
{
    uint64_t t[16]={0}; 
    uint8_t i=0 ;
    uint64_t temp = 0;
    uint8_t len=strlen(str);
    if(len>16) return -1;
     for(i=0;i<len;i++) 
     {
        t[i]=str_to_int(str[i]);//隐含准话U8--U64
        temp |= t[i]<<((len-1-i)*4);
     }
    return temp;   
}


////抽象延时函数 完美解决延时中需要喂狗的尴尬
//void sys_delay(uint32_t ms)
//{
//	if(ms < 500)
//	{
//		osDelay(ms);
//	}
//	else
//	{
//		for(uint16_t i = 0 ; i <  ms/500 ; i++)
//		{
//			osDelay(500);
//            //HAL_IWDG_Refresh(&hiwdg);
//			task_keep_alive(TASK_ALL_BIT); 
//		}
//		osDelay( ms%500);
//        //HAL_IWDG_Refresh(&hiwdg);
//		task_keep_alive(TASK_ALL_BIT);         
//	}
//}


////获取任务堆栈
//void read_task_stack(char const *name , xTaskHandle taskHandle)
//{
//    unsigned portBASE_TYPE uxHighWaterMark; 
//    
//    uxHighWaterMark = uxTaskGetStackHighWaterMark(taskHandle);

//    log(WARN,"[%s]stack surplus = %d.\r\n" ,name,uxHighWaterMark);
//    
//}

        
////系统软件复位
//void soft_system_resert( const char *funs )
//{
//    log(WARN,"System has been resert form %s\n" , funs);
//    NVIC_SystemReset();
//}



#endif
